package com.zzx.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.zzx.common.core.page.PageDomain;
import com.zzx.common.core.page.TableSupport;
import com.zzx.utils.sql.SqlUtil;

public class PageUtils {

    /**
     * 创建分页对象
     *
     * @param pageNum  当前页码
     * @param pageSize 每页数量
     * @return 分页对象
     */
    public static <T> IPage<T> createPage(int pageNum, int pageSize) {
        return new Page<>(pageNum, pageSize);
    }

    public static Page createMPJPage(int pageNum, int pageSize) {
        return new Page<>(pageNum, pageSize);
    }

    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }
}
